//
//  TTDianpuUploadImageCollectionViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/11/7.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDianpuUploadImageCollectionViewCell.h"
#import <UIImageView+WebCache.h>

@interface TTDianpuUploadImageCollectionViewCell ()

@property (nonatomic, strong) TTDianpuUploadImageModel *model;
@property (nonatomic, assign) BOOL canModify;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *deleteBtn;

@end

@implementation TTDianpuUploadImageCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupSubViews];
        
    }
    return self;
}

// 删除图片
- (void)deleteImageAction
{
    if ([self.delegate respondsToSelector:@selector(imageCell:deleteModel:)]) {
        [self.delegate imageCell:self deleteModel:self.model];
    }
}

- (void)setModel:(TTDianpuUploadImageModel *)model canModify:(BOOL)canModify
{
    self.model = model;
    self.canModify = canModify;
    if (model.imageUrlString.length > 0) {
        NSString *urlStr = [model.imageUrlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"f_placeholder_img"] options:SDWebImageRetryFailed];
    } else {
        self.imageView.image = [UIImage imageNamed:model.imageName];
    }
    
    if (model.imageName.length > 0) {
        self.deleteBtn.hidden = YES;
    } else {
        self.deleteBtn.hidden = !canModify;
    }
    
}

- (void)setupSubViews
{
    _imageView = [[UIImageView alloc] init];
    _imageView.layer.cornerRadius = 5;
    _imageView.layer.masksToBounds = YES;
    _imageView.backgroundColor = UIColorHex(F1F5F6);
    [self.contentView addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(14);
        make.left.mas_equalTo(7);
        make.right.equalTo(self.contentView).mas_offset(-7);
        make.bottom.equalTo(self.contentView);
    }];
    
    _deleteBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"reddelete"] target:self action:@selector(deleteImageAction)];
    [_deleteBtn setContentEdgeInsets:UIEdgeInsetsMake(5, 10, 0, 0)];
    [self.contentView addSubview:_deleteBtn];
    [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self.contentView);
        make.width.height.mas_equalTo(30);
    }];
    
    
    
}

@end
